//
//  BIDMainViewController.m
//  商务厅
//
//  Created by mal on 13-12-9.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDMainViewController.h"
#import "BIDCollectionCellView.h"
#import "BIDNotifyViewController.h"
#import "BIDLeaveViewController.h"
#import "BIDOutingReportViewController.h"

#import "BIDAddressBookViewController.h"
#import "BIDEMailViewController.h"
#import "BIDNewsListViewController.h"
#import "BIDAnnouncementListViewController.h"
#import "BIDVideoListViewController.h"
#import "BIDBusinessRemindViewController.h"
#import "BIDSettingViewController.h"

#import "BIDNoticeKindViewController.h"
#import "BIDInfoBriefViewController.h"
#import "BIDMeetingSignListViewController.h"
#import "BIDMeetingSignViewController.h"
#import "BIDApplyAndReportViewController.h"
#import "BIDConstructionListViewController.h"
#import "BIDPartyMainViewController.h"

#import "BIDMeetingListViewController.h"
#import "BIDAnnouncementListViewController.h"

#import <PgyUpdate/PgyUpdateManager.h>

#import "BPush.h"
//百度推送绑定channelId
static NSString *bindingChannelIdURL = @"pushUserAndroid!addPushUser.action";

//通知公告角标
static NSString *noticeBadgeNumberURL = @"noticeAndroid!gainNotReadSum.action";
//大项目建设角标
static NSString *constructionBadgeNumberURL = @"itemBuildingAndroid!gainNotRead.action";
//邮件角标
static NSString *emailBadgeNumberURL = @"emailReplyAndroid!gainEmailNotRead.action";

//请示报告角标
static NSString *leaveBadgeNumberURL = @"leaveAndroid!gainLeaveNotRead.action";
//出差报备角标
static NSString *outtingBadgeNumberURL = @"outingAndroid!checkNew.action";
//工作提醒角标
static NSString *workBadgeNumberURL = @"workAndroid!gainWorkNotRead.action";

@interface BIDMainViewController ()

@end

@implementation BIDMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[PgyUpdateManager sharedPgyManager] checkUpdate];
    self.title = @"手机秘书";
    [self baiduPushSettting];
    self.navigationController.navigationBarHidden = NO;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    //
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSNumber *fontSizeValue = [userDefaults objectForKey:@"font"];
    [AppDelegate setFontSize:[fontSizeValue intValue]];
    //
    _menuArr = @[@{@"title":@"会议管理", @"imgName":@"notice.png"},
                 @{@"title":@"公告管理", @"imgName":@"announcementManage.png"},
                 @{@"title":@"信息简报", @"imgName":@"brief.png"},
                 @{@"title":@"外出报备", @"imgName":@"outingReport.png"},
                 @{@"title":@"内部邮件", @"imgName":@"email.png"},
                 @{@"title":@"通讯录", @"imgName":@"addressBook.png"},
                 @{@"title":@"会议签到", @"imgName":@"meetingSign.png"},
                 @{@"title":@"通知管理", @"imgName":@"caixintong.png"}];
//    @{@"title":@"鄄城新闻", @"imgName":@"news.png"},
//    @{@"title":@"请示报告", @"imgName":@"apply.png"},
    //
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"qr_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toSign)];
    self.navigationItem.leftBarButtonItem = barButtonItem;
    //
    UIImageView *imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"setting.png"]];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(settingBtnHandler)];
    [imgView2 addGestureRecognizer:tap];
    UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:imgView2];
    self.navigationItem.rightBarButtonItem = barButtonItem2;
    //
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotificationHandler) name:PUSH_NOTIFICATION object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self refreshBadge];
}

- (void)updateTheme
{
}

- (void)baiduPushSettting
{
    //百度推送
    [BPush bindChannelWithCompleteHandler:^(id result, NSError *error) {
        // 需要在绑定成功后进行 settag listtag deletetag unbind 操作否则会失败
        if (result)
        {
            NSString *strChannelId = [BPush getChannelId];
            NSLog(@"channelId=%@\r\n", strChannelId);
            //
            NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], bindingChannelIdURL];
            NSString *strPost = [[NSString alloc] initWithFormat:@"pushUser.userId=%@&pushUser.channelId=%@&pushUser.deviceType=1", [AppDelegate getUserId], strChannelId];
            BIDDataCommunication *dc = [[BIDDataCommunication alloc] init];
            [dc getDataFromNet:strRequestURL postValue:strPost completionHandler:^(id obj, NSError *err){
                if(obj && !err)
                {
                    if([obj isKindOfClass:[NSDictionary class]])
                    {
                        NSDictionary *dic = (NSDictionary*)obj;
                        NSNumber *value = dic[@"isSuccess"];
                        if([value intValue] == 1)
                        {
                        }
                    }
                }
                else
                {}
            }];
        }
    }];
    
}

- (void)pushNotificationHandler
{
    [self refreshBadge];
}

- (void)refreshBadge
{
    //
    NSString *strPost = [[NSString alloc] initWithFormat:@"userId=%@", [AppDelegate getUserId]];
    //index代表界面中功能模块所在位置
    //通知公告
    NSDictionary *dictionary1 = @{@"index":@0, @"url":noticeBadgeNumberURL, @"param_request":strPost, @"param_response":@"notReadSum"};
    //信息简报
    NSDictionary *dictionary2 = @{@"index":@2, @"url":constructionBadgeNumberURL, @"param_request":strPost, @"param_response":@"notRead"};
    //请示报告
//    NSDictionary *dictionary3 = @{@"index":@3, @"url":leaveBadgeNumberURL, @"param_request":strPost, @"param_response":@"total"};
    //外出报备
    NSDictionary *dictionary4 = @{@"index":@3, @"url":outtingBadgeNumberURL, @"param_request":strPost, @"param_response":@"recvNotRead"};
    //内部邮件
    NSDictionary *dictionary5 = @{@"index":@4, @"url":emailBadgeNumberURL, @"param_request":strPost, @"param_response":@"total"};
    //彩信通//通知管理
    NSDictionary *dictionary6 = @{@"index":@7, @"url":@"itemBuildingAndroid!gainNotRead.action", @"param_request":strPost, @"param_response":@"notRead"};
    NSArray *arr = @[dictionary1, dictionary2, dictionary4, dictionary5, dictionary6];
    [self getCornerMarkCount:arr];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)toSign
{
    BIDMeetingSignViewController *vc = [[BIDMeetingSignViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

//系统设置
- (void)settingBtnHandler
{
    BIDSettingViewController *vc = [[BIDSettingViewController alloc] initWithNibName:@"BIDSettingViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark BIDPassValueDelegate
//通知改变视图背景
- (void)passValue:(int)param1
{
    [self.bgImgView setImage:[UIImage imageNamed:[AppDelegate getViewBgImg]]];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger index = [indexPath row];
    UIViewController *vc = nil;
    switch(index)
    {
        case 0:
        {
            //会议管理
            vc = [[BIDMeetingListViewController alloc] init];
        }
            break;
        case 1:
        {
            //公告管理
            vc = [[BIDAnnouncementListViewController alloc] init];
        }
            break;
        case 2:
        {
            //信息简报
            vc = [[BIDInfoBriefViewController alloc] init];
        }
            break;
//        case 3:
//        {
//            //请示报告
//            vc = [[BIDApplyAndReportViewController alloc] init];
//        }
//            break;
        case 3:
        {
            //外出报备
            vc = [[BIDOutingReportViewController alloc] init];
        }
            break;
        case 4:
        {
            //内部邮件
            vc = [[BIDEMailViewController alloc] init];
        }
            break;
        case 5:
        {
            //通讯录
            vc = [[BIDAddressBookViewController alloc] init];
        }
            break;
        case 6:
        {
            //会议签到
            vc = [[BIDMeetingSignListViewController alloc] init];
        }
            break;
//        case 8:
//        {
//            //鄄城新闻
//            vc = [[BIDNewsListViewController alloc] init];
//        }
//            break;
//        case 9:
//        {
//            //党建
//            vc = [[BIDPartyMainViewController alloc] init];
//        }
//            break;
        case 7:
        {
            //彩信通//通知管理
            vc = [[BIDConstructionListViewController alloc] init];
        }
            break;
    }
    [self.navigationController pushViewController:vc animated:YES];
}

@end
